package v1

import (
	"context"
	"wxpay/internal/dto"
	"wxpay/internal/service"
	wxpay "wxpay/pkg/api/v1"
)

// 二级商户账户提现申请 POST /api/v1/wxpay/mch/balance/withdraw
func (s *Server) SubMchWithdraw(ctx context.Context, request *wxpay.SubMchWithdrawRequest) (*wxpay.SubMchWithdrawResponse, error) {
	withdrawDto := &dto.SubMchWithDrawDTO{
		BrandID:       request.BrandId,
		MchID:         request.MchId,
		SubMchID:      request.SubMchId,
		BizWithdrawID: request.BizNo,
		Amount:        request.Amount,
		Remark:        request.Remark,
		BankMemo:      request.BankMemo,
	}
	wd, err := service.NewWithdrawService(s.d).WithdrawApply(ctx, withdrawDto)
	if err != nil {
		return nil, err
	}
	return &wxpay.SubMchWithdrawResponse{OutRequestNo: wd.OutRequestNo}, nil
}

// 二级商户账户提现查询 GET /api/v1/wxpay/mch/balance/withdraw/{biz_no}
func (s *Server) SubMchWithdrawQuery(ctx context.Context, request *wxpay.SubMchWithdrawQueryRequest) (*wxpay.SubMchWithdrawQueryResponse, error) {
	withdraw, err := service.NewWithdrawService(s.d).QueryByBizNo(ctx, request.BizNo)
	if err != nil {
		return nil, err
	}
	return &wxpay.SubMchWithdrawQueryResponse{
		SubMchId:     withdraw.Status,
		MchId:        withdraw.MchID,
		Amount:       withdraw.Amount,
		BizNo:        withdraw.BizWithdrawID,
		Remark:       withdraw.Remark,
		BankMemo:     withdraw.BankMemo,
		Status:       withdraw.Status,
		WxWithdrawId: withdraw.WxWithdrawID,
		OutRequestNo: withdraw.OutRequestNo,
	}, nil
}
